package ast.nodes;

import regEx.RegExEvaluator;
import regEx.dfa.DFA;
import regEx.finiteAutomata.NFA;
import scanner.Token;
import ast.ASTNode;
import ast.Evaluation;

public class RegExNode extends ASTNode {

    private RegExEvaluator regExEvaluator;

    public RegExNode(Token data)
    {
        super(data);
        this.regExEvaluator = new RegExEvaluator();
    }

    public RegExNode() {
        super();
        this.regExEvaluator = new RegExEvaluator();
    }

    public NFA getNFA() throws Exception
    {
    	return this.regExEvaluator.getNFA(this);
    }
    
    public DFA getDFA() throws Exception
    {
    	return this.regExEvaluator.getDFA(this);
    }
    
    @Override
    public String toString(int depth) {
        String parametersStr = "";
        for (ASTNode child : getChildren()) {
            parametersStr += child.toString(depth+1);
        }

        // TODO this is just temporary debug statement
//        try {
//
//            System.err.println("\n NFA:");
//            System.err.println("\n" + this.regExEvaluator.getNFA(this) + "\n");
//            System.err.println("\n DFA:");
//            System.err.println("\n" + this.regExEvaluator.getDFA(this) + "\n");
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//
//        System.err.println("Testing 'ab': " + this.regExEvaluator.evaluateExpression("ab"));
//        System.err.println("Testing 'ababababab': " + this.regExEvaluator.evaluateExpression("abababab"));
//        System.err.println("Testing 'abc': " + this.regExEvaluator.evaluateExpression("abc"));
//
//        // end temporary debug
        return indent(depth) + "RegEx:" + parametersStr;
    }

    //TODO quick fix, we need to think about matching groups
    public boolean match(String s) throws Exception
    {
    	regExEvaluator.getNFA(this);
    	regExEvaluator.getDFA(this);
    	return regExEvaluator.evaluateExpression(s);
    }
    
    /**
     * Call the RegExToDFA class with this.children to create a DFA, then run the string
     * on the DFA and return "1" if it matches, or "0" if it doesnt.
     *
     * TODO should it return 1 or 0 as a string, or should we make a change?
     */
    @Override
    public void run(Evaluation input) throws Exception {

    }
}
